import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api
Page({

  /**
    * 页面的初始数据
    */
   data: {
     id:'',
    isBack:false,
    auditText:'',
     active:0,
     list:[],
     steps: [],
   },
  /**编辑 */
  edit(e){
    wx.navigateTo({
      url: '../costEdit/costEdit?id='+this.data.id,
    })
  },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
     let id = options.id;
    if(api.isBlank(id)){
      wx.showToast({
        title: '未找到ID',
        icon: 'none',
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },3000)
   } else{
      this.requestData(id);
      api.setBackFlag();
    }
   },
 
  /**请求数据 */
  requestData(id){
    let that = this;
    api.curlPost('?cla=costApp&fun=detail',{id:id}).then((res)=>{
      that.setData({
        id:res.data.result.info.id,
        info:res.data.result.info,
        list:res.data.result.list,
        active:res.data.result.info.active,
        steps:res.data.result.info.auditRecord,
        isBack:false,
        auditText:'',
      })
    })
  },
  
/**提交审批 */
commitAudit(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'费用报销',act:'start'}).then((res)=>{
    api.curlPost('?cla=auditApp&fun=operate',{id:id,body:res.data.result,act:'start'}).then((back)=>{
      if(back.data.auditType =='审批人自选'){
        let type = 'addAuditor';
        wx.navigateTo({
          url: '../chooseStaff/chooseStaff?id='+id+'&type='+type+'&flowName=费用报销',
        })
      }else{
        Toast('提交成功');
        that.requestData(id);
      }
    })
  })
},
/**审批 */
audit(e){
  let that = this;
  let id = this.data.id;
  let type = e.currentTarget.dataset.type;
 
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'费用报销',act:'audit'}).then((res)=>{
    let para = {
      state:type,
      id:id,
      body:res.data.result,
      text:that.data.auditText
    }
    api.curlPost('?cla=auditApp&fun=auditLayerEdit',para).then((back)=>{
      Toast('审核成功');
      that.requestData(id);
    })
  })
},
/**撤销 */
cancel(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=getPara',{id:id,flowName:'费用报销',act:'cancel'}).then((res)=>{
    api.curlPost('?cla=auditApp&fun=operate',{id:id,body:res.data.result,act:'cancel'}).then((back)=>{
      
      that.requestData(id);
      Toast(back.warn);
    })
  })
},

/**增加审批人或转交给其他人审批 */
transfer(e){
  let id = this.data.id;
  let type = e.currentTarget.dataset.type;
  wx.navigateTo({
    url: '../chooseStaff/chooseStaff?id='+id+'&type='+type+'&flowName=费用报销',
  })

},

/**催办 */
press(){
  let id = this.data.id;
  let that = this;
  api.curlPost('?cla=auditApp&fun=operate',{id:id,act:'press'}).then((back)=>{
      
    that.requestData(id);
    Toast(back.warn);
  })

},
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {
 
   },
 
   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
    if(this.data.isBack){
      this.requestData(this.data.id)
    }
   },
 
   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {
 
   },
 
   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {
 
   },
 
   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
 
   },
 
   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {
 
   },
 
   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {
 
   }
 })